/**
 * Created by ChenYong on 2016-08-16.
 */

apiready = function () {
    juicer.set({
        'tag::operationOpen': '{@',
        'tag::operationClose': '}',
        'tag::interpolateOpen': '&{',
        'tag::interpolateClose': '}',
        'tag::noneencodeOpen': '$${',
        'tag::noneencodeClose': '}',
        'tag::commentOpen': '{#',
        'tag::commentClose': '}',
        'strip': false,
        'cache': false
    });

    // 设置ios7的标题栏字体变亮，全局用一个就行了
    api.setStatusBarStyle({
        style : 'light'
    });
    var statusBarAppearance = api.statusBarAppearance; // 比如： true
    /**************************************************[前端部分]**************************************************/
    // 头部的克隆
    // var $cloneHeader = $("#header").clone();
    $(".tab-item").click(function () {
        var $this = $(this);
        var index = $this.data("index");

    });
    //点击行高亮
    $(".center-list-con li").on("touchstart",function(){
        $(this).css("background-color","#EEEEEE");
    });
    $(".center-list-con li").on("touchend",function(){
        $(this).css("background-color","#fff");
    });
    //滑动也高亮
    $(".center-list-con li").on("touchmove",function(){
        $(this).css("background-color","#fff");
    });
//    轮播图初始化
    var home_swiper = {};
    var job_swiper = {};

    function oneSwiper() {
        home_swiper = new Swiper('#home-swiper', {
            pagination: '#home-swiper-pagination',
            paginationClickable: true,
            autoplayDisableOnInteraction: false,
            autoplay: 3000,
            speed: 600,
            loop: true,
            observer: true,
            observeParents: true,
            onClick: function (swiper) {
                //console.log(swiper.activeIndex);
            }
        });
    }

    function twoSwiper() {
        job_swiper = new Swiper('#job-swiper', {
            pagination: '#job-swiper-pagination',
            paginationClickable: true,
            autoplay: 3000,
            autoplayDisableOnInteraction: false,
            speed: 600,
            loop: true,
            observer: true,
            observeParents: true,
            onClick: function (swiper) {
                //console.log(swiper.activeIndex);
            }
        });
    }

    var flag1 = false;
    var flag2 = false;

    $(document).on("click", ".tab-link", function () {
        if (flag1 && ($(this).attr('data-index') == 0) && !isEmpty(home_swiper) && !isEmpty(job_swiper)) {
            //开始轮播
            home_swiper.startAutoplay();
            job_swiper.stopAutoplay();
            //home_swiper.updatePagination();
            //home_swiper.update(true);
            home_swiper.activeIndex = 1;
        } else if (flag2 && ($(this).attr('data-index') == 2) && !isEmpty(home_swiper) && !isEmpty(job_swiper)) {
            //开始轮播
            job_swiper.startAutoplay();
            home_swiper.stopAutoplay();
            //job_swiper.update(true);
            job_swiper.activeIndex = 1;
            //job_swiper.updatePagination();
        }
    });
    //下拉菜单 变量声明
    var idData = {};//idData.id1 idData.id2
    var qiye_id = {};//qiye_id.id1
    var price_id = {};//qiye_id.id1
//    二级选择下拉菜单
    (function($){
        $.chooseTree=function(el,data,fun){
            //点击切换遮罩层
            function addZheZhao(){
                if($("body").find(".zhezhao").length==0 && $("body").find(".zhezhao1").length==0){
                    $(".zhezhaoNode").append("<div class='zhezhao'></div>").find(".zhezhao")
                        .css({
                            "position":"fixed",
                            "width":"100%",
                            "height":"100%",
                            "background-color":"#000",
                            "opacity":"0.5",
                            "top":"5rem",
                            "z-index":"20"
                        });
                    //$("nav").append("<div class='zhezhao1'></div>").find(".zhezhao1")
                    //    .css({
                    //        "position":"fixed",
                    //        "width":"100%",
                    //        "height":"50px",
                    //        "background-color":"#000",
                    //        "opacity":"0.5",
                    //        "bottom":"0",
                    //        "z-index":"20"
                    //    });
                }else{
                    $(".zhezhao").remove();
                }
            };

            //自动添加省略号
            var addellipsis=function(text){
                var charCode = -1,realLen= 0,qitaNum=0,hanziNum= 0;
                for(var i=0;i<text.length;i++){
                    charCode=text.charCodeAt(i);
                    if(charCode >= 0 && charCode <= 128){
                        realLen += 1;
                        qitaNum+=1;
                    }else{
                        realLen += 2;
                        hanziNum+= 1;
                    }
                    if(realLen==9||realLen==10){
                        break;
                    }
                }
                if(realLen==9||realLen==10){
                    text=text.substring(0,(qitaNum+hanziNum-1))+"...";
                }
                return text;
            }

            //点击要生成列表的元素
            $(el).on('click',function(){
                if(data && data.length>1){
                    //设置点击时的初始样式
                    $(this).siblings().attr('data-tog',0)
                        .find("span").css("color","#615959")
                        .end().children("div").remove();

                    if($(this).attr('data-tog')==1){
//                    还原颜色
                        $(this).find("span").css("color","#615959");
                        $(".zhezhao").remove();
                    }else{
                        //点亮颜色
                        $(this).find("span").css("color","#0894ec");
                        $(".zhezhao").remove();
                        addZheZhao();
                    }
//                addZheZhao();
//                每次清空内容
                    var parentUlHtml="";
                    var firstUlhtml="";

//                //根据data-tog的值来判断是否 显示或隐藏
                    if($(this).attr('data-tog')==undefined || $(this).attr('data-tog')==0){
//                    //显示
                        for(var i=0;i<data.length;i++){
                            parentUlHtml+="<li data-id1="+i+">"+data[i].category_name+"</li>";
                        }
//
//                    //获取第一个data2的COURSE_NAME数据
                        var firstData=data[1].data2
                        for(var r in firstData){
                            firstUlhtml+="<li data-id2="+r+">"+firstData[r].COURSE_NAME+"</li>";
                        }
                        var allUlHtml="<div>" +
                            "<div class='duTitleWrap'>"+
                            "<ul class='duTitle1'>"+parentUlHtml+"</ul>" +
                            "<ul class='duTitle2'>"+firstUlhtml+"</ul>"+
                            "</div>"+
                            "<div class='shouqi'>收起</div>"
                        "</div>";
//
                        $(this).append(allUlHtml)
                            .attr('data-tog',1)
                            .find(".duTitle1 li:eq(1)")
                            .css({"background":"#fff","color":"#0894ec"});

                        if($('.shouqi').css('border-top-width')=='0px'){
                            //如果左边ul高度低于右边ul高度，右边底色为灰色，并加上黑色是哪个底部border
                            if($('.duTitle1').height()<$('.duTitle2').height()){
                                $('.duTitleWrap').css('background-color','#EFEFEF');
                                $('.shouqi').css('border-top','1px solid #dedede');
                                $('.duTitle2 li:last').css('border-bottom-color','transparent');
                            }else{
                                $('.shouqi').css('border-top','1px solid #dedede');
                                $('.duTitleWrap').css('background-color','#fff');
                                $('.duTitle1 li:last').css('border-bottom-color','transparent');
                            }
                        }

                        //当前显示的标题一 左边加上border突出显示
                        $(".duTitle1 li:eq(1)").css("border-left","3px solid #0894ec");
                    }else{
//                    隐藏
                        $(this).attr('data-tog',0).children('div').remove()
                            .end().children('.icon').css("color",'#615959')
                            .prev().css("color",'#615959');
                    }
                }else{
                    alert('没有数据');
                }

                //防止点击这一层时事件冒泡
                $('.duTitleWrap').click(function(){
                    return false;
                });

                //首次二级标题点击事件处理
                $(".duTitle2 li").click(function(){
                    var childData=data[1].data2;
                    idData.id2=childData[$(this).attr("data-id2")].COURSE_ID;
                    idData.id1=data[1].category_id;

                    var subText=$(this).text();

//              移除下拉框
                    $(el).children("div").remove().end()
                        .attr('data-tog',0)
                        .find('span:first')
                        .text(addellipsis(subText)).css('color','#615959')
                        .next().css('color','#615959');
//                    移除遮罩
                    $(".zhezhao").remove();
//                addZheZhao();
                    //回调函数
                    if(fun){
                        fun();
                    }
                    return false;
                });

                //点击一级标题
                $(".duTitle1 li").click(function(){

                    if(data[$(this).attr('data-id1')].data2==undefined){
                        idData.id1=data[$(this).attr('data-id1')].category_id;
//                    自动添加省略号
                        var subText=$(this).text();

//                    移除下拉框
                        $(el).children("div").remove().end()
                            .attr('data-tog',0)
                            .find('span:first')
                            .text(addellipsis(subText)).css('color','#615959')
                            .next().css('color','#615959');
//                    移除遮罩
                        $(".zhezhao").remove();
//                    addZheZhao();
                        //回调函数
                        if(fun){
                            fun();
                        }
                    }else{
                        var childUlHtml="";
                        var childData=data[$(this).attr("data-id1")].data2;
//              储存第一级标题的id
                        idData.id1=data[$(this).attr("data-id1")].category_id;
                        for(var j=0;j<childData.length;j++){
                            childUlHtml+="<li data-id2="+j+">"+
                                childData[j].COURSE_NAME+
                                "</li>";
                        }
                        childUlHtml="<ul class='duTitle2'>"+childUlHtml+"</ul>";
                        $(".duTitle2").replaceWith(childUlHtml);
                        $(this).css({
                            'background-color':'#fff','color':'#0894ec',
                            "border-left":"3px solid #0894ec"
                        })
                            .siblings()
                            .css({
                                'background-color':'#EFEFEF',"color":'#615959',
                                "border-left":"none"
                            });


                        if($('.shouqi').css('border-top-width')=='1px'){
                            //如果左边ul高度低于右边ul高度，右边底色为灰色，并加上黑色是哪个底部border
                            if($('.duTitle1').height()<$('.duTitle2').height()){
                                $('.duTitleWrap').css('background-color','#EFEFEF');
                                $('.shouqi').css('border-top','1px solid #dedede');
                                $('.duTitle2 li:last').css('border-bottom-color','transparent');
                                $('.duTitle1 li:last').css('border-bottom-color','#dedede');
                            }else{
                                $('.duTitleWrap').css('background-color','#fff');
                                $('.duTitle1 li:last').css('border-bottom-color','transparent');
                            }
                        }

                        //点击二级标题
                        $(".duTitle2 li").click(function(){

                            idData.id2=childData[$(this).attr("data-id2")].COURSE_ID;

//                    自动添加省略号
                            var subText=$(this).text();

//                    移除下拉框
                            $(el).children("div").remove().end()
                                .attr('data-tog',0)
                                .find('span:first')
                                .text(addellipsis(subText)).css('color','#615959')
                                .next().css('color','#615959');
//                    移除遮罩
                            $(".zhezhao").remove();
                            //回调函数
                            if(fun){
                                fun();
                            }
                            return false;
                        });
                    }
                    return false;
                })
            });
        }
    })(jQuery);

//一级选择下拉菜单
    (function($){
        //点击切换遮罩层
        function addZheZhao(){
            if($("body").find(".zhezhao").length==0 && $("body").find(".zhezhao1").length==0){
                $(".zhezhaoNode").append("<div class='zhezhao'></div>").find(".zhezhao")
                    .css({
                        "position":"fixed",
                        "width":"100%",
                        "height":"100%",
                        "background-color":"#000",
                        "opacity":"0.5",
                        "top":"5rem",
                        "z-index":"20"
                    });
                //$("nav").append("<div class='zhezhao1'></div>").find(".zhezhao1")
                //    .css({
                //        "position":"fixed",
                //        "width":"100%",
                //        "height":"50px",
                //        "background-color":"#000",
                //        "opacity":"0.5",
                //        "bottom":"0",
                //        "z-index":"20"
                //    });
            }else{
                $(".zhezhao").remove();
            }
        };
        $.simpleChoose=function(el,data,fun){
            $(el).click(function(){
                if(data && data.length>0){
                    var parentUlHtml="";
                    //设置点击时的初始样式
                    $(this).siblings().attr('data-tog',0)
                        .find("span").css("color","#615959")
                        .end().children("div").remove();

                    if($(this).attr('data-tog')==1){
//                    还原颜色
                        $(this).find("span").css("color","#615959");
                        $(".zhezhao").remove();
                    }else{
                        //点亮颜色
                        $(this).find("span").css("color","#0894ec");
                        $(".zhezhao").remove();
                        addZheZhao();
                    }

//                设置默认值

                    if($(this).attr('data-tog')==undefined || $(this).attr('data-tog')==0){

                        for(var i=0;i<data.length;i++){
                            if(el==".simpleChoose1"){
                                parentUlHtml+="<li data-title1Id="+i+">"+data[i].name+"</li>";
                            }else if(el==".simpleChoose2"){
                                parentUlHtml+="<li data-title1Id="+i+">"+data[i].price+"</li>";
                            }

                        }
                        parentUlHtml="<div class='pos'><div><ul class='simpleUl1'>"+parentUlHtml+"</ul></div><div>收起</div></div>"
                        $(this).append(parentUlHtml)
                            .attr('data-tog',1)
                            .find('.pos').css("position","absolute")
                            .find(".simpleUl1").css({"max-height":"7rem","overflow-y":"auto"});

//                    根据上次记忆的序号，并高亮显示其中文字

                        if(qiye_id.index && el==".simpleChoose1"){
                            $(".simpleUl1 li").eq(qiye_id.index).css({
                                "color":"#0894ec",
                                "border-left":"3px solid #0894ec"
                            });
                        }

                        if(price_id.index && el==".simpleChoose2"){
                            $(".simpleUl1 li").eq(price_id.index).css({
                                "color":"#0894ec",
                                "border-left":"3px solid #0894ec"
                            });
                        }

                        $(".simpleUl1 li").click(function(){

                            if(el==".simpleChoose1"){
                                qiye_id.id1=data[$(this).attr('data-title1Id')].qiyeid;
                                qiye_id.index=$(this).attr('data-title1Id');
                            }else if(el==".simpleChoose2"){
                                price_id.id1=data[$(this).attr('data-title1Id')].priceid;
                                price_id.index=$(this).attr('data-title1Id');
                            }
//                        自动添加省略号
                            var subText=$(this).text();
                            if(subText.length>4){
                                subText=$(this).text().substring(0,4)+"...";
                            }
                            $(el).children("div").remove().end()
                                .attr('data-tog',0)
                                .find('span:first')
                                .text(subText).css('color','#615959')
                                .next().css('color','#615959');
                            $(".zhezhao").remove();
//                        addZheZhao();
                            //回调函数
                            if(fun){
                                fun();
                            }
                            return false;
                        });
                    }else{
                        $(this).attr('data-tog',0).children('div').remove()
                            .end().children('.icon').css("color",'#615959');
                    }
                }else{
                    alert("没有数据");
                }
            });
        }
    })(jQuery);

    //点击工具栏 控制头部是否要显示 以及在培训以及就业页面头部增加 搜索按钮 并赋予他们不同的标识符
    $("nav a").click(function () {
        var index = $(this).data("index");
        //显示头部 隐藏头部 函数 参数1代表显示 0代表隐藏
        function shouHeader(arg) {
            if (arg == 1) {
                $('header').css("display", 'block');
                $('.bar-tab~.content').css('top', '2.2rem')
            } else if (arg == 0) {
                $('header').css("display", 'none');
                $('.bar-tab~.content').css('top', '0')
            }
        }

        //增加头部搜索图标 并给其 传入标识符data-tag
        function setSearch(showTag, dataTag) {
            if (showTag == 0) {
                $('header span').remove();
            } else if (showTag == 1) {
                $('header span').remove();
                $('header').append('' +
                    '<span class="icon iconfont icon-sousuo header-search-btn" data-tag="' + dataTag + '">' +
                    '</span>' +
                    '');
            }
        }

        if (index == 0) {
            shouHeader(1)
            setSearch(0);
            $("header .title").text('首页');
            $('header').append('' +
                '<span class="icon iconfont header-search-btn icon-email" data-tag="' + index + '">' +
                '</span>' +
                '');
        } else if (index == 1) {
            shouHeader(1);
            setSearch(1, 1);
            $("header .title").text('培训');
        } else if (index == 2) {
            shouHeader(1);
            setSearch(1, 2)
            $("header .title").text('企业招聘');
        } else {
            shouHeader(0);
        }
    });

    $('#page').on('click', '.header-search-btn', function () {
        var $this = $(this);
        var tag = $this.data("tag");

        // 搜索培训
        if (tag == 1) {
            openWin("train-search", "train-search.html", null);
        // 搜索就业
        }else if (tag == 2) {
            openWin("job-search", "job-search.html", null);
        }else if(tag == 0){
            openWin("info-center", "info-center.html", null);
        }
    });

    //点击进入我的作业duer
    $("#my-homework").click(function(){
        if(isLogin()){
            openWin("my-homework", "my-homework.html", {
                appBackground:'rgba(238,238,238,1.0)',
                bgColor : 'rgba(238,238,238,1.0)'
            });
        }else{
            toLogin();
        }
    });
    /**************************************************[/前端部分]**************************************************/


    /**************************************************[蒙继勇部分]**************************************************/
        //轮播图片
    var lunbosql = "select a.*,(select H.channel_type from cms_channel H WHERE H.channel_id=B.channel_id) CHANNELTYPE from cms_content a ,cms_channe_content b where a.content_id=b.CONTENT_ID and b.CHANNEL_ID=20 order by a.PUBLSIH_DATE";
    dbQuery(1, 3, lunbosql, true, function (ret, err) {
        if (isObject(ret) && (ret.statusCode == 0)) {
            var lunboData = {
                lunboList: ret.result,
                url: getUrl()
            };

            $.each(lunboData.lunboList, function(i, e) {
                if (isEmpty(e.TITLE_IMG)) {
                    e.TITLE_IMG = "../images/no_jpg.jpg";
                } else {
                    e.TITLE_IMG = "{0}{1}".format(lunboData.url, e.TITLE_IMG);
                }
            });

            $("#home-lunbo").html(juicer($("#lunbo-tpl").html(), lunboData));

            flag1 = true;

            oneSwiper();

        } else {
            flag1 = false;
        }
    });

    //资讯
    var zixunsql = "select a.*,CASE WHEN CHAR_LENGTH(a.TITLE)>13 THEN CONCAT(LEFT(a.TITLE,13),'...') ELSE a.TITLE END TITLEED," +
        "(select H.channel_type from cms_channel H WHERE H.channel_id=B.channel_id) channel_type" +
        " from cms_content a ,cms_channe_content b where a.content_id=b.CONTENT_ID" +
        " and b.CHANNEL_ID='9' order by a.PUBLSIH_DATE";
    // 咨询页数
    var mjyPage = 1;
    dbQuery(mjyPage, 10, zixunsql, true, function (ret, err) {
        if (isObject(ret) && (ret.statusCode == 0)) {
            var zixunData = {
                zixunList: ret.result,
                url:getUrl()
            };

            // 添加默认图片
            $.each(zixunData.zixunList, function(i, e) {
                if (isEmpty(e.TITLE_IMG)) {
                    e.TITLE_IMG = "../images/no_png.png";
                } else {
                    e.TITLE_IMG = "{0}{1}".format(zixunData.url, e.TITLE_IMG);
                }
            });

            $("#zixun-ul").html(juicer($("#zixun-tpl").html(), zixunData));
        }
    });

    // 下拉刷新最新咨询
    $(document).on("refresh", "#tab1 .pull-to-refresh-content", function () {
        mjyPage++;

        // 获取就业单位
        dbQuery(mjyPage, 10, zixunsql, false, function (ret, err) {
            if (isObject(ret) && (ret.statusCode == 0)) {
                var zixunData = {
                    zixunList: ret.result,
                    url:getUrl()
                };

                if (zixunData.zixunList.length > 0) {
                    // 添加默认图片
                    $.each(zixunData.zixunList, function(i, e) {
                        if (isEmpty(e.TITLE_IMG)) {
                            e.TITLE_IMG = "../images/no_png.png";
                        } else {
                            e.TITLE_IMG = "{0}{1}".format(zixunData.url, e.TITLE_IMG);
                        }
                    });

                    $("#zixun-ul").prepend(juicer($("#zixun-tpl").html(), zixunData));
                } else {
                    toast("已加载完毕");

                    mjyPage--;
                }
            }

            $.pullToRefreshDone("#tab1 .pull-to-refresh-content");
        });
    });

    $("#page").on("click", ".mjy-zixun", function () {
        var $this = $(this);
        var zixunId = $this.data("zixunId");

        openWin("news-detail", "news-detail.html", {
            zixunId: zixunId
        });
    });

    //点击首页轮播图片进入资讯详情页面
    $("#home-lunbo").on("click", "#home-lunbo-img", function () {
        var $this = $(this);
        var zixunId = $this.data("zixunId");
        openWin("news-detail", "news-detail.html", {
            zixunId: zixunId
        });
    });
    /**************************************************[/蒙继勇部分]**************************************************/


    /**************************************************[蔡涛部分]**************************************************/
    var ctPage=1;

    function mainSql(sql) {
        dbQuery(ctPage, 10, sql, true, function (ret, err) {

            if (isObject(ret) && (ret.statusCode == 0)) {

                var arr = [];
                var sb = [];
                $.each(ret.result, function (index, ems) {

                    var num=0;
                    if(ems.END_DATE&&ems.BEGIN_DATE){
                        num = (Date.parse(new Date(ems.END_DATE.replace(/-/g, "/"))) - Date.parse(new Date(ems.BEGIN_DATE.replace(/-/g, "/")))) / (30 * 24 * 60 * 60 * 1000);
                    }
                    arr.push(parseInt(num));
                    if (ems.SCALE_FLAG != null && ems.SCALE_FLAG != "" && ems.ENABLED_FLAG != "" && ems.ENABLED_FLAG != null && ems.ENABLED_FLAG == 1) {

                        sb.push(ems.STD_FEE * ems.SCALE_FLAG);

                    } else {
                        sb.push(ems.STD_FEE);
                    }

                });

                var courseData = {
                    courseList: ret.result,
                    url: getUrl(),
                    num: arr,
                    price: sb
                };

                // 添加默认图片
                $.each(courseData.courseList, function(i, e) {
                    if (isEmpty(e.CLASS_TYPE_PIC)) {
                        e.CLASS_TYPE_PIC = "../images/no_jpg.jpg";
                    } else {
                        e.CLASS_TYPE_PIC = "{0}{1}".format(courseData.url, e.CLASS_TYPE_PIC);
                    }
                });

                $("#course-class-list").html(juicer($("#select-course-list").html(), courseData));
            }
        });
    }

    //下拉刷新
    $(document).on("refresh", "#tab2 .pull-to-refresh-content", function (e) {
        ctPage++;
        dbQuery(ctPage, 10, sql, true, function (ret, err) {
            if (isObject(ret) && (ret.statusCode == 0)) {
                var arr = [];
                var sb = [];
                $.each(ret.result, function (index, ems) {
                    var num = (new Date(ems.END_DATE).getTime() - new Date(ems.BEGIN_DATE).getTime()) / (30 * 24 * 60 * 60 * 1000);
                    arr.push(parseInt(num));
                    if (ems.SCALE_FLAG != null && ems.SCALE_FLAG != "" && ems.ENABLED_FLAG != "" && ems.ENABLED_FLAG != null && ems.ENABLED_FLAG == 1) {

                        sb.push(ems.STD_FEE * ems.SCALE_FLAG);

                    } else {
                        sb.push(ems.STD_FEE);
                    }

                });
                var courseData = {
                    courseList: ret.result,
                    url: getUrl(),
                    num: arr,
                    price: sb
                };

                if(courseData.courseList.length>0){
                    // 添加默认图片
                    $.each(courseData.courseList, function(i, e) {
                        if (isEmpty(e.CLASS_TYPE_PIC)) {
                            e.CLASS_TYPE_PIC = "../images/no_jpg.jpg";
                        } else {
                            e.CLASS_TYPE_PIC = "{0}{1}".format(courseData.url, e.CLASS_TYPE_PIC);
                        }
                    });

                    $("#course-class-list").prepend(juicer($("#select-course-list").html(), courseData));
                }else{
                    toast("已加载完毕");
                    ctPage--;
                }
            }
            $.pullToRefreshDone("#tab2 .pull-to-refresh-content");
        });
    })

    var sql = "select a.*,b.SCALE_FLAG,b.ENABLED_FLAG  from B_CLASS_TYPE a  LEFT JOIN B_CLASS_DISCOUNT b ON a.CLASS_TYPE_ID = b.CLASS_TYPE_ID";

    mainSql(sql);

    var arr_course = [];

    //一级科目查询
    var sql2 = "select * from B_CATEGORY";
    dbQuery(1, 10, sql2, false, function (ret, err) {
        if (isObject(ret) && (ret.statusCode == 0)) {
            var courseData1 = {
                courseList: ret.result
            };

            $.each(ret.result, function (index, course) {
                var result1 = new Object();
                result1.category_id = course.CATEGORY_ID;
                result1.category_name = course.CATEGORY_NAME;
                arr_course.push(result1);
            });

            //二级科目查询
            var sql3 = "select * from b_course";
            dbQuery(1, 10, sql3, false, function (ret, err) {
                if (isObject(ret) && (ret.statusCode == 0)) {
                    var courseData2 = {
                        courseList: ret.result
                    };

//                    toast(arr.length);
                    $.each(arr_course, function (index, courseItem) {

                        var categoryId = courseItem.category_id;
                        var arr2 = [];
                        $.each(ret.result, function (index, secondItem) {

                            if (secondItem.CATEGORY_ID === categoryId) {
                                arr2.push(secondItem);
                            }
                        });
                        courseItem.data2 = arr2;
                    });

                    //向头部加入一条数据 代表全部
                    arr_course.unshift({
                        //这个id表示所有类别
                        "category_id": "-999",
                        "category_name": "全部类别"

                    });
                    $.chooseTree('.choose-tree', arr_course, function () {
                        sqlStrEnd()

                    });
                }
            });

            /*  var courseNode = document.getElementById('select-first-categroy').innerHTML;
             var courseHtml = juicer(courseNode, courseData1);

             $("#course-class-list").html(courseHtml);*/
        }
    });

    var arr3 = [];

    var sql3 = "select *  from B_ORGAN";
    dbQuery(1, 10, sql3, false, function (ret, err) {
        if (isObject(ret) && (ret.statusCode == 0)) {
            var courseData5 = {
                courseList: ret.result
            };

            $.each(ret.result, function (index, organ) {
                var organObj = new Object();
                organObj.qiyeid = organ.ORGAN_ID;
                organObj.name = organ.ORGAN_NAME;
                arr3.push(organObj);
            });


            $.simpleChoose('.simpleChoose1', arr3, function () {
                sqlStrEnd();
            });
        }
    });

    var arr_price = [];
    for (var i = 0; i < 4; i++) {
        var priceObj = new Object();
        if (i === 0) {
            priceObj.price = "全部";
        } else if (i === 1) {
            priceObj.price = "4000~5000";
        } else if (i === 2) {
            priceObj.price = "5000~6000";
        } else {
            priceObj.price = "6000~8000";
        }
        priceObj.priceid = i;
        arr_price.push(priceObj);
    }
    $.simpleChoose('.simpleChoose2', arr_price, function () {
        sqlStrEnd()
    });

    function sqlStrEnd() {

        var str = "";
        var str1 = sql;

        var arrTemp = [];

        if (idData.id1 === "-999") {

        } else {
            if (isNotEmpty(idData.id2)) {
                arrTemp.push("a.COURSE_ID LIKE '" + idData.id2 + "'");
            }
        }


        if (isNotEmpty(qiye_id.id1)) {
            arrTemp.push("a.ORGAN_ID LIKE '" + qiye_id.id1 + "'");
        }

        if (isNotNull(price_id.id1)) {

            //toast(price_id.id1);
            if (price_id.id1 === 1) {
                arrTemp.push("a.STD_FEE BETWEEN 4000 AND 5000");
            } else if (price_id.id1 === 2) {
                arrTemp.push("a.STD_FEE BETWEEN 5000 AND 6000");
            } else if (price_id.id1 === 3) {
                arrTemp.push("a.STD_FEE BETWEEN 6000 AND 8000");
            }
        }
        str = arrTemp.join(" and ");
        if (isNotEmpty(str)) {
            str1 += " where " + str;
        }
        mainSql(str1);
    }

    $("#page").on("touchend", ".ct-main-train", function () {
        var $this = $(this);
        var classTypeId = $this.data("classTypeId");

        openWin("course", "course.html", {
            classTypeId: classTypeId
        });

    });
    /**************************************************[/蔡涛部分]**************************************************/


    /**************************************************[陈勇部分]**************************************************/
        // 获取轮播 SQL
    var cySql1 = "SELECT A.*, (SELECT H.CHANNEL_TYPE FROM CMS_CHANNEL H WHERE H.CHANNEL_ID = B.CHANNEL_ID) CHANNELTYPE ";
    cySql1 += "FROM CMS_CONTENT A, CMS_CHANNE_CONTENT B WHERE A.CONTENT_ID = B.CONTENT_ID AND B.CHANNEL_ID = 20 ORDER BY A.PUBLSIH_DATE";

    // 获取轮播数据
    dbQuery(1, 3, cySql1, true, function (ret, err) {
        if (isObject(ret) && (ret.statusCode == 0)) {
            var lunboData = {
                lunboList: ret.result,
                url: getUrl()
            };

            // 添加默认图片
            $.each(lunboData.lunboList, function(i, e) {
                if (isEmpty(e.TITLE_IMG)) {
                    e.TITLE_IMG = "../images/no_jpg.jpg";
                } else {
                    e.TITLE_IMG = "{0}{1}".format(lunboData.url, e.TITLE_IMG);
                }
            });

            $("#cy-lunbo").html(juicer($("#cy-lubo-tpl").html(), lunboData));

            flag2 = true;

            twoSwiper();
            job_swiper.stopAutoplay();
        } else {
            flag2 = false;
        }
    });

    // 获取就业单位 SQL
    var cySql2 = "SELECT * FROM E_COOPERATION_UNIT T";
    /** 页数 */
    var cyPage2 = 1;
    // 获取就业单位
    dbQuery(cyPage2, 10, cySql2, true, function (ret, err) {

        if (isObject(ret) && (ret.statusCode == 0)) {
            var companyData = {
                companyList: ret.result,
                url: getUrl()
            };

            // 添加默认图片
            $.each(companyData.companyList, function(i, e) {
                if (isEmpty(e.PIC_PATH)) {
                    e.PIC_PATH = "../images/no_png.png";
                } else {
                    e.PIC_PATH = "{0}{1}".format(companyData.url, e.PIC_PATH);
                }
            });

            $("#cy-company").html(juicer($("#cy-company-tpl").html(), companyData));
        }
    });

    // 下拉刷新就业
    $(document).on("refresh", "#tab3 .pull-to-refresh-content", function (e) {
        cyPage2++;

        // 获取就业单位
        dbQuery(cyPage2, 10, cySql2, false, function (ret, err) {
            if (isObject(ret) && (ret.statusCode == 0)) {
                var companyData = {
                    companyList: ret.result,
                    url: getUrl()
                };

                if (companyData.companyList.length > 0) {
                    // 添加默认图片
                    $.each(companyData.companyList, function(i, e) {
                        if (isEmpty(e.PIC_PATH)) {
                            e.PIC_PATH = "../images/no_png.png";
                        } else {
                            e.PIC_PATH = "{0}{1}".format(companyData.url, e.PIC_PATH);
                        }
                    });

                    $("#cy-company").prepend(juicer($("#cy-company-tpl").html(), companyData));
                } else {
                    toast("已加载完毕");

                    cyPage2--;
                }
            }

            $.pullToRefreshDone("#tab3 .pull-to-refresh-content");
        });
    });

    $("#page").on("click", ".cy-company", function () {
        var $this = $(this);
        var unitId = $this.data("unitId");

        openWin("company-info", "company-info.html", {
            unitId: unitId
        });
    });
    $("#cy-lunbo").on("click", "#cy-lunbo-img", function () {
        var $this = $(this);
        var zixunId = $this.data("zixunId");
        openWin("news-detail", "news-detail.html", {
            zixunId: zixunId
        });
    });

    /**************************************************[/陈勇部分]**************************************************/

    /**************************************************[个人中心部分]**************************************************/
        // 修改头像 SQL
    var updateHeadImgSql = "UPDATE SU_STAFF T SET T.PHOTO_PATH = '{0}' WHERE T.STAFF_ID = '{1}'";
    // 上传头像的标志：true-可以上传头像；false-不可以上传头像
    var uploadHeadImgFlag = true;

    // 监听登录事件
    api.addEventListener({
        name: "loginEvent"
    }, function (ret, err) {
        var tmpUserInfo = getLoginUserInfo();

        if (ret.value.flag === "myFocusFlag") {
            openWin("my-focus-guide", "my-focus-guide.html", null);
        }

        // 无头像
        if (isEmpty(tmpUserInfo.PHOTO_PATH)) {
            $("#user_head_img").attr("src", "../images/appportrait2.png");

            // 有头像
        } else {
            $("#user_head_img").attr("src", "{0}upload/{1}".format(getUrl(), tmpUserInfo.PHOTO_PATH));
        }

        $("#login-btn").hide();
        $("#login-info-btn").show().html(tmpUserInfo.STAFF_NAME);
    });

    //监听用户信息更改
    api.addEventListener({
        name: 'infoUpdateEvent'
    }, function (ret, err) {
        var tmpUserInfo = getLoginUserInfo();
        $("#login-info-btn").show().html(tmpUserInfo.STAFF_NAME);

        $("#user_head_img").attr("src", "{0}upload/{1}".format(getUrl(), tmpUserInfo.PHOTO_PATH));

    });

    // 监听退出登录事件
    api.addEventListener({
        name: "logoutEvent"
    }, function (ret, err) {
        $("#user_head_img").attr("src", "../images/appportrait2.png");

        $("#login-btn").show();
        $("#login-info-btn").hide().html("");
    });

    // 已登录
    if (isLogin()) {
        var tmpUserInfo = getLoginUserInfo();

        // 无头像
        if (isEmpty(tmpUserInfo.PHOTO_PATH)) {
            $("#user_head_img").attr("src", "../images/appportrait2.png");

            // 有头像
        } else {
            $("#user_head_img").attr("src", "{0}upload/{1}".format(getUrl(), tmpUserInfo.PHOTO_PATH));
        }

        $("#login-btn").hide();
        $("#login-info-btn").show().html(tmpUserInfo.STAFF_NAME);

        // 未登录
    } else {
        $("#user_head_img").attr("src", "../images/appportrait2.png");

        $("#login-btn").show();
        $("#login-info-btn").hide().html("");
    }

    // 点击显示登录界面
    $("#login-btn").click(function () {
        openWin("user-login", "user-login.html", null);
    });

    // 点击显示登录信息
    $("#login-info-btn").click(function () {
        if (isLogin()) {
            openWin("person-info", "person-info.html", null);
        } else {
            toLogin();
        }
    });


    //我的缴费记录
    $("#my-pay-btn").click(function () {
        if (isLogin()) {
            openWin("pay.log", "pay-log.html", null);
        } else {
            toLogin();
        }
    });

    //我的班级
    $("#my-grade-btn").click(function () {
        if (isLogin()) {
            openWin("my-class", "my-class.html", null);
        } else {
            toLogin();
        }
    });

    //班级选课
    $("#my-choose-class").click(function () {
        if (isLogin()) {
            openWin("my-class", "my-choose-class.html", null);
        } else {
            toLogin();
        }
    });

    //我的考勤
    $("#my-checking-btn").click(function () {
        if (isLogin()) {
            openWin("my-kaoqin", "my-kaoqin.html", null);
        } else {
            toLogin();
        }
    });

    //设置
    $("#set-btn").click(function () {
        openWin("my-center-settings", "my-center-settings.html", null);
    });

    // 我的关注
    $(".my-focus-load").click(function () {
        if (isLogin()) {
            openWin("my-focus-guide", "my-focus-guide.html", null);
        } else {
            toLogin({flag: "myFocusFlag"});
        }
    });

    // 我的简历
    $(".my-resume").click(function () {
        if (isLogin()) {
            openWin("my-resume", "my-resume.html", null);
        } else {
            toLogin();
        }
    });

    // 已投简历的岗位
    $('.my-focus-resume').click(function(){
        if (isLogin()) {
            openWin("my-focus-resume", "my-focus-resume.html", null);
        } else {
            toLogin();
        }
    });

    // 点击上传用户头像
    $("#user_head_img").click(function () {
        var $this = $(this);

        if (isLogin()) {
            // 获取登录用户
            var tmpUserInfo = getLoginUserInfo();

            if (uploadHeadImgFlag && isObject(tmpUserInfo)) {
                getPicture(300, 300, "library", function (ret, err) {
                    if (isObject(ret)) {
                        var url = ret.data;
                        var uuid = getUUID();

                        if (isNotEmpty(url)) {
                            uploadHeadImgFlag = false;

                            // 上传头像
                            uploadHeadImage(tmpUserInfo.STAFF_ID, url, uuid, true, function (ret, err, remotePath) {
                                // 上传成功
                                if (isObject(ret) && (ret.statusCode == 0)) {
                                    var headImgPath = ret.result;

                                    // 修改头像
                                    dbExecute(updateHeadImgSql.format(headImgPath, tmpUserInfo.STAFF_ID), true, function (ret, err) {
                                        uploadHeadImgFlag = true;

                                        // 执行成功
                                        if (isObject(ret) && (ret.statusCode == 0)) {
                                            toast("上传头像成功");

                                            tmpUserInfo.PHOTO_PATH = headImgPath;

                                            // 重新缓存登录用户信息
                                            setStaffInfo("loginUserInfo", tmpUserInfo);

                                            $this.attr("src", "{0}upload/{1}".format(getUrl(), tmpUserInfo.PHOTO_PATH));

                                            // 执行失败
                                        } else {
                                            setTimeout(function () {
                                                toast("上传头像失败，稍后再试");
                                            }, 3000);
                                        }
                                    });
                                } else {
                                    uploadHeadImgFlag = true;

                                    setTimeout(function () {
                                        toast("上传头像失败，稍后再试");
                                    }, 3000);
                                }
                            });
                        } else {
                            uploadHeadImgFlag = true;
                        }
                    } else {
                        uploadHeadImgFlag = true;
                    }
                });
            }
        } else {
            toLogin();
        }
    });
    /**************************************************[/个人中心部分]**************************************************/
};